﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Collections;
using System.Text.RegularExpressions;
using System.Threading;
using Winista.Text.HtmlParser.Lex;

using WTH = Winista.Text.HtmlParser;
using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Filters;
using Winista.Text.HtmlParser.Util;

namespace Spider
{
    public partial class Posts : Form
    {
        public Posts()
        {
            InitializeComponent();
        }

        private void Posts_Load(object sender, EventArgs e)
        {
            // this.webBrowser1.Url = new Uri("http://www.yyq777.com/");

        }


        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

            if (!e.Url.ToString().Contains("forum-15")) return;

            string content = webBrowser1.Document.Body.InnerHtml;

            IEnumerator enu = new Regex(@"(thread-\d*-1-1).*(\.html)")
               .Matches(content)
               .GetEnumerator();

            while (enu.MoveNext() && enu.Current != null)
            {
                //WebBrowser borwser = new WebBrowser();
                //borwser.Url = new Uri("http://www.yyq777.com/thread-72127-1-1.html");
                //borwser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(borwser_DocumentCompleted);

                string uri = "http://www.yyq777.com/" + ((Match)(enu.Current)).Value;

                if (uri.Length > 50) continue;
                if (uri.Contains("thread-161650-1-1.html")) continue;
                if (uri.Contains("thread-72127-1-1.html")) continue;
                if (uri.Contains("thread-143508-1-1.html")) continue;
                if (uri.Contains("thread-50548-1-1.html")) continue;
                if (uri.Contains("thread-20884-1-1.html")) continue;

                WebBrowser borwser = new WebBrowser();
                borwser.Url = new Uri(uri);
                borwser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(borwser_DocumentCompleted);

                Thread.Sleep(1000);

                Application.DoEvents();
            }
        }

        void borwser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

            string content = (sender as WebBrowser).Document.Body.InnerHtml.ToUpper();

            Lexer lexer = new Lexer(content);
            WTH.Parser parser = new WTH.Parser(lexer);
            NodeFilter filter = new HasAttributeFilter("CLASS", "T_MSGFONT");
            //  NodeFilter HH = new TagNameFilter("H2");

            NodeList node = parser.Parse(filter);
            if (node.ElementAt(0) != null)
            {
                var title = node.ElementAt(0).PreviousSibling.PreviousSibling.ToPlainTextString();
                var conent = node.ElementAt(0).ToPlainTextString();

                SQLHelper.ExecuteNonQuery(string.Format("insert into url_temp(title,content) values('{0}','{1}')", title, conent));
            }

        }



        private void button1_Click(object sender, EventArgs e)
        {

            var tt = SQLHelper.ExecuteDataset("SELECT distinct title , content from url_temp where title<>'' ");

            foreach (DataRow item in tt.Tables[0].Rows)
            {
                if (item["content"].ToString().Count() < 10000)
                {
                    string insert = string.Format(
                        @"insert into topics(title,content,uv,reply_count) values('{0}','{1}',0,0)", item["title"].ToString(), item["content"].ToString());

                    MySqlHelper.ExecuteNonQuery(insert);
                }
            }

            MessageBox.Show("OKKKKKKKKKKK");

        }
    }
}

